#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//createvfs.h"
#include "..//include//n_ary.h"

Tree insert(Tree root, char filepath[]){
   
   char str[100];
   char newstr1[50];
   char newstr2[50]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree sib=cur_tree;
   Tree new;
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;

   
   	if(searchTree(root, filepath) == 1){
   		return root;
   	}
   	
   	if(strcmp(filepath,"/")==0) return root;
      // Split the path based on '/'
      
      while(filepath[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split(filepath,pos);
         
         if(num>(strlen(filepath)-1)) num = strlen(filepath)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=filepath[i];
            j++;
         }
         newstr2[j]='\0';     

	 /*k = 0;
	 for(i=0; i<=pos; i++){
		abspath[k] = filepath[i];
		k++;

	 }
	 abspath[k]='\0';*/
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(filepath)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      }   
	
         
      

      //Actual insertion into the node

      cur=first;
      cur_tree=root->left;
      sib=root->left;
      prev_tree=root;     
      while(count>0){
	  if(cur_tree!=NULL){ 
        	if(strcmp(cur_tree->node_label,cur->entry) == 0){  
            		cur=cur->next;
            		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			sib=cur_tree;
			count=count-1;
			
			if(count==1 && cur_tree==NULL){   
			       new=(Tree)malloc(sizeof(struct nary_tree));
			       strcpy(new->node_label,cur->entry);
			       strcpy(new->path,cur->abspath);			       
	 
			       new->left=NULL;
			       new->right=NULL;
			       prev_tree->left=new;
			       count=count-1;			       
            		}
			else{  
			       if(count==1){  
				  prev_tree=cur_tree;
				  cur_tree=cur_tree->right;				  
			       }
            		}
        	}
        	else{   
			prev_tree=cur_tree;
            		cur_tree=cur_tree->right;
        	}
	}
	else{   
		if(sib==cur_tree){   //insertion of left child
		 	new=(Tree)malloc(sizeof(struct nary_tree));			
		 	strcpy(new->node_label,cur->entry);	
			strcpy(new->path,cur->abspath);		
			
			
		 	new->left=NULL;
		 	new->right=NULL;			
		 	prev_tree->left=new;			
		 	prev_tree=new;			
		 	cur_tree=new->left;			
		 	sib=cur_tree;			
		 	count = count-1;			
			cur=cur->next;
		}

		else{		//insertion of right sibling
			new=(Tree)malloc(sizeof(struct nary_tree));
		 	strcpy(new->node_label,cur->entry);
			strcpy(new->path,cur->abspath);
		 	new->left=NULL;
		 	new->right=NULL;
		 	prev_tree->right=new;		 	
			prev_tree=new;
			cur_tree=new->left;
			sib=cur_tree;
			count=count-1;
			cur=cur->next;
		}				      
	}
     
      }
	       
    
return root;
}

int split(char *str,int pos){
   int j,i;
   j=0;
   for(i=pos; i<strlen(str);i++){
      if(str[i]=='/' || str[i]=='"') return i-1;
   }
   
   return i;
}

int searchTree(Tree root, char dir_name[]){  // 0-->Not found; 1-->Found
   char str[100];
   char newstr1[50];
   char newstr2[50]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree sib=cur_tree;
   Tree new;
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;
   
   
   
	
	while(dir_name[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split(dir_name,pos);
         
         if(num>(strlen(dir_name)-1)) num = strlen(dir_name)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=dir_name[i];
            j++;
         }
         newstr2[j]='\0';     

	 /*k = 0;
	 for(i=0; i<=pos; i++){
		abspath[k] = dir_name[i];
		k++;

	 }
	 abspath[k]='\0';*/
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(dir_name)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      } 
      
      
      cur=first;
      cur_tree=root->left;
      sib=root->left;
      prev_tree=root;     
      while(count>0){
	  if(cur_tree!=NULL){ 
        	if(strcmp(cur_tree->node_label,cur->entry) == 0){  
            		cur=cur->next;
            		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			sib=cur_tree;
			count=count-1;			
			
        	}
        	else{   
			prev_tree=cur_tree;
            		cur_tree=cur_tree->right;
        	}
	}
	else{   
		return 0;		
			      
	}
     
      } 
      
      return 1; 
}


/*
char *get_ext(char s[])
{
	char temp[20];
	static char temp2[20];
	int len,i,t;
	i=0;

	len=strlen(s);
	t=len-1;
	while(s[t]!='.'){
		temp[i]=s[t];
		t--;
		i++;
	}

	temp[i]='\0';

	i=0;
	len=strlen(temp);
	t=len-1;
	while(t>=0){
		temp2[i]=temp[t];
		t--;
		i++;
	}
	temp2[i]='\0';


	return temp2;

}*/


void RecDisplay(Tree root)
{	
	
	if(root!=NULL)
	{        
		//printf("%s , %s \n",root->node_label,root->path);				
		RecDisplay(root->right);		
		RecDisplay(root->left);
		
	}
	
}

